package com.ship.boats.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.SpBoatsVoyageMessage;
import com.ship.boats.domain.dto.QueryBoatsVoyageRq;
import com.ship.boats.domain.dto.QueryPortNameRq;
import com.ship.boats.domain.vo.*;
import com.ship.boats.service.BoatsVoyageMessageService;
import com.ship.boats.util.ExportDownloadUtil;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>BoatsVoyageMessageController</b>
 *
 * @description: BoatsVoyageMessageController <br>
 * @date: 2024/4/12 16:48 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "航次列表模块")
@RestController
public class BoatsVoyageMessageController {
    private static final Logger logger = LoggerFactory.getLogger(BoatsVoyageMessageController.class);

    @Autowired
    private BoatsVoyageMessageService boatsVoyageMessageService;

    @Autowired
    private ExportDownloadUtil exportDownloadUtil;

    @ApiOperation("查询航次列表搜索条件")
    @GetMapping("/query/boatsVoyage/Conditions")
    public JsonResult queryBoatsVoyageConditions(@RequestParam String type){
        List<String> list = boatsVoyageMessageService.queryBoatsVoyageConditions(type);
        return JsonResult.success(list);
    }

    @ApiOperation("根据国家查询港口名称")
    @PostMapping("/query/portName/byCountry")
    public JsonResult queryPortNameByCountry(@RequestBody QueryPortNameRq rq){
        QueryBoatsPortInfo boatsPortList = boatsVoyageMessageService.queryPortNameByCountry(rq);
        return JsonResult.success(boatsPortList);
    }

    @ApiOperation("分页查询航次列表信息")
    @PostMapping("/query/boatsVoyageMessage/list")
    public JsonResult queryBoatsVoyageList(@RequestBody QueryBoatsVoyageRq rq) throws IllegalAccessException {
        if (CollectionUtil.isNotEmpty(rq.getDischargePortList())){
            List<String> dischargePortList = new ArrayList<>();
            rq.getDischargePortList().forEach(item -> {
                String dischargePort = HtmlUtils.htmlUnescape(item);
                dischargePortList.add(dischargePort);
            });
            rq.setDischargePortList(dischargePortList);
        }
        if (CollectionUtil.isNotEmpty(rq.getLoadPortList())){
            List<String> loadPortList = new ArrayList<>();
            rq.getLoadPortList().forEach(item -> {
                String loadPort = HtmlUtils.htmlUnescape(item);
                loadPortList.add(loadPort);
            });
            rq.setLoadPortList(loadPortList);
        }
        logger.info("查询航次列表信息返回入参:{}"+ JSONUtil.parse(rq));
        QueryReceiveCountryInfo receiveCountryInfo = boatsVoyageMessageService.queryBoatsVoyageList(rq);
        logger.info("分页查询航次列表返回结果:{}"+JSONUtil.parse(receiveCountryInfo));
        return JsonResult.success(receiveCountryInfo);
    }

    @ApiOperation("查询航次列表收货国/发货国数据分析")
    @PostMapping("/query/dataAnalyze")
    public JsonResult queryDataAnalyze(@RequestBody QueryBoatsVoyageRq rq) throws IllegalAccessException {
        QueryReceiveCountryInfo receiveCountryInfo = boatsVoyageMessageService.queryDataAnalyze(rq);
        logger.info("查询航次列表收货国/发货国数据分析返回结果:{}"+JSONUtil.parse(receiveCountryInfo));
        return JsonResult.success(receiveCountryInfo);
    }

    @ApiOperation("根据展示类型查询贸易统计数据")
    @PostMapping("/query/distinguishType")
    public JsonResult queryDistinguishType(@RequestBody QueryBoatsVoyageRq rq){
        if (CollectionUtil.isNotEmpty(rq.getDischargePortList())){
            List<String> dischargePortList = new ArrayList<>();
            rq.getDischargePortList().forEach(item -> {
                String dischargePort = HtmlUtils.htmlUnescape(item);
                dischargePortList.add(dischargePort);
            });
            rq.setDischargePortList(dischargePortList);
        }
        if (CollectionUtil.isNotEmpty(rq.getLoadPortList())){
            List<String> loadPortList = new ArrayList<>();
            rq.getLoadPortList().forEach(item -> {
                String loadPort = HtmlUtils.htmlUnescape(item);
                loadPortList.add(loadPort);
            });
            rq.setLoadPortList(loadPortList);
        }
        List<QueryVolumeTradeIfo> volumeTradeList = boatsVoyageMessageService.queryDistinguishType(rq);
        return JsonResult.success(volumeTradeList);
    }

    @ApiOperation("根据展示类型查询量统计数据 - 新")
    @PostMapping("/query/volumeTrade")
    public JsonResult queryVolumeTrade(@RequestBody QueryBoatsVoyageRq rq) throws IllegalAccessException {
        if (CollectionUtil.isNotEmpty(rq.getDischargePortList())){
            List<String> dischargePortList = new ArrayList<>();
            rq.getDischargePortList().forEach(item -> {
                String dischargePort = HtmlUtils.htmlUnescape(item);
                dischargePortList.add(dischargePort);
            });
            rq.setDischargePortList(dischargePortList);
        }
        if (CollectionUtil.isNotEmpty(rq.getLoadPortList())){
            List<String> loadPortList = new ArrayList<>();
            rq.getLoadPortList().forEach(item -> {
                String loadPort = HtmlUtils.htmlUnescape(item);
                loadPortList.add(loadPort);
            });
            rq.setLoadPortList(loadPortList);
        }
        List<QueryVoyageTradeInfo> volumeTradeList = boatsVoyageMessageService.queryVolumeTrade(rq);
        return JsonResult.success(volumeTradeList);
    }

    @ApiOperation("导出航次列表信息")
    @PostMapping("/export/boatsVoyage")
    public JsonResult exportBoatsVoyage(@RequestBody QueryBoatsVoyageRq rq){
        if (CollectionUtil.isNotEmpty(rq.getDischargePortList())){
            List<String> dischargePortList = new ArrayList<>();
            rq.getDischargePortList().forEach(item -> {
                String dischargePort = HtmlUtils.htmlUnescape(item);
                dischargePortList.add(dischargePort);
            });
            rq.setDischargePortList(dischargePortList);
        }
        if (CollectionUtil.isNotEmpty(rq.getLoadPortList())){
            List<String> loadPortList = new ArrayList<>();
            rq.getLoadPortList().forEach(item -> {
                String loadPort = HtmlUtils.htmlUnescape(item);
                loadPortList.add(loadPort);
            });
            rq.setLoadPortList(loadPortList);
        }
        List<QueryBoatsVoyageMessageInfo> boatsVoyageMessageList = boatsVoyageMessageService.exportBoatsVoyage(rq);
        List<String> tableList = new ArrayList<>();
        tableList.add("shipName");
        tableList.add("mmsi");
        tableList.add("imo");
        tableList.add("voyageStatus");
        tableList.add("totalCargoProspect");
        tableList.add("unitMeasurement");
        tableList.add("loadInportTime");
        tableList.add("loadInportDraught");
        tableList.add("totalLoadVoyageTime");
        tableList.add("loadRegion");
        tableList.add("loadCountry");
        tableList.add("loadPort");
        tableList.add("loadArea");
        tableList.add("loadTerminal");
        tableList.add("loadBerth");
        tableList.add("loadInBerthTime");
        tableList.add("loadOutBerthTime");
        tableList.add("loadOutPortTime");
        tableList.add("loadOutPortDraught");
        tableList.add("dischargeInPortTime");
        tableList.add("dischargeInPortDraught");
        tableList.add("dischargeRegion");
        tableList.add("dischargeCountry");
        tableList.add("dischargePort");
        tableList.add("dischargeArea");
        tableList.add("dischargeTerminal");
        tableList.add("dischargeBerth");
        tableList.add("dischargeInBerthTime");
        tableList.add("dischargeOutBerthTime");
        tableList.add("dischargeOutPortTime");
        tableList.add("dischargeOutPortDraught");
        tableList.add("shipTypeGroup");
        tableList.add("operator");
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("船舶名称");
        tableNameList.add("MMSI");
        tableNameList.add("IMO");
        tableNameList.add("航次状态");
        tableNameList.add("预估总运量");
        tableNameList.add("计量单位");
        tableNameList.add("装货到港时间");
        tableNameList.add("装货到港吃水");
        tableNameList.add("预装航行时间总计");
        tableNameList.add("装货大洲");
        tableNameList.add("装货国家");
        tableNameList.add("装货港口");
        tableNameList.add("装货大区");
        tableNameList.add("装货接收站");
        tableNameList.add("装货泊位");
        tableNameList.add("装货靠泊时间");
        tableNameList.add("装货离泊时间");
        tableNameList.add("装货离港时间");
        tableNameList.add("装货离港吃水");
        tableNameList.add("卸货到港时间");
        tableNameList.add("卸货到港吃水");
        tableNameList.add("卸货大洲");
        tableNameList.add("卸货国家");
        tableNameList.add("卸货港口");
        tableNameList.add("卸货大区");
        tableNameList.add("卸货接收站");
        tableNameList.add("卸货泊位");
        tableNameList.add("卸货靠泊时间");
        tableNameList.add("卸货离泊时间");
        tableNameList.add("卸货离港时间");
        tableNameList.add("卸货离港吃水");
        tableNameList.add("船舶类型分组");
        tableNameList.add("运营船东");
        Field[] fields = QueryBoatsVoyageMessageInfo.class.getDeclaredFields();
        String url = exportDownloadUtil.exportUtilDate(boatsVoyageMessageList,fields,
                tableList,tableNameList,
                "航次列表");
        return JsonResult.success(url);
    }
}
